/** 
*
* @author  jiwf 
* @date 创建时间：2016年10月15日 下午9:28:20 
* @version 1.0 
* @since  
*  
*/
package com.xbstar.web;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.xbstar.bean.AffixInfo;
import com.xbstar.dao.AffixInfoRepository;
import com.xbstar.tools.Utils;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/file")
public class ImgUpload {
	@Autowired
	private AffixInfoRepository affixDao;
	@RequestMapping(value = "/saveImg") 
	@ResponseBody
    public String saveImage(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest req,HttpServletResponse resp) {  
		resp.setHeader("Access-Control-Allow-Origin", "*"); //允许哪些url可以跨域请求到本域
		resp.setHeader("Access-Control-Allow-Methods","POST"); //允许的请求方法，一般是GET,POST,PUT,DELETE,OPTIONS
		resp.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
		
		JSONObject logicResultData = new JSONObject();
		if(file==null){
			logicResultData.put(UserPackage.STATUS,UserPackage.LOST_PARAM);
			return logicResultData.toString();
		}
		try {  	
			String basedir ="C:\\tomacat\\webapps";
			//String basedir ="C:\\tmpimg";
				String fileName =System.currentTimeMillis()+".png";
				String filePathName=basedir+"\\result\\"+fileName;
		        File targetFile = new File(basedir+"\\result");  
		        if(!targetFile.exists()){  
		            targetFile.mkdirs();  
		        }  
				String relationDir = "/result";
				BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(filePathName)));
				out.write(file.getBytes());
	              out.flush();
	              out.close();  	            
				AffixInfo af = new AffixInfo();
				String belongTo=req.getParameter("belongTo");
				if(belongTo==null||belongTo.equals("")){
					belongTo = Utils.getUUID();
				}
				af.setBelongTo(belongTo);
				af.setName(fileName);
				af.setUrl("http://106.14.30.199"+relationDir+"/"+fileName);
				//af.setUrl(""+relationDir+"/"+fileName);
				try {
					affixDao.save(af);
					JSONObject tjson=new JSONObject();
					tjson.put("belongTo", belongTo);
					tjson.put("src", af.getUrl());
					tjson.put("id", af.getId());
					logicResultData.put(UserPackage.STATUS, UserPackage.SUCCESS);
					logicResultData.put(UserPackage.DATA,tjson);
				} catch (Exception e) {
					logicResultData.put(UserPackage.STATUS, UserPackage.SERVER_ERROR);
				}
			
        } catch (Exception e) {  
        	try {
				logicResultData.put(UserPackage.STATUS, UserPackage.SERVER_ERROR);

			} catch (JSONException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
        }  
	  
        return logicResultData.toString();  
    }  
	@RequestMapping("/delImg")
	public String delImg(@RequestParam("id")Long id){
		JSONObject json=new JSONObject();
		try {
			affixDao.delete(id);
			json.put(UserPackage.STATUS, UserPackage.SUCCESS);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			json.put(UserPackage.STATUS, UserPackage.REDO);
			e.printStackTrace();
		}
		return json.toString();
	}
	
	
}
